<template>
  <div>
    <!-- 顶部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card视图区域 -->
    <el-card class="box-card">
        <!-- 头部搜索部分 -->
        <el-row :gutter="20">
        <el-col :span="10">
            <el-input placeholder="请输入内容" v-model="queryInfo.query">
                <el-button slot="append" icon="el-icon-search" @click="searchOrder"></el-button>
            </el-input>
        </el-col>
        </el-row>

        <!-- table 区域 -->
      <el-table :data="orderList" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="250px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款"  v-slot="slot">
            <template>
                <el-tag :type="slot.row.order_pay === '1'  ? 'success' : 'danger'">{{slot.row.order_pay === '1' ? '已付款' : '未付款'}}</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间" min-width="200px" v-slot="slot">
            <template>
                {{slot.row.create_time | dateFormat}}
            </template>
        </el-table-column>
        <el-table-column label="操作" v-slot="slot" min-width="180px">
            <el-button type="primary" icon="el-icon-edit" @click="editAddress(slot.row)" size="mini"></el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="queryLocation"></el-button>
        </el-table-column>
      </el-table>
    
        <!-- page 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!-- 修改地址弹框 -->
      <el-dialog
        @close="resetForm"
        title="修改"
        :visible.sync="editDialogVisible">
          <el-form :model="editForm" :rules="editRules" ref="editFormRef" label-width="100px">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              :options="cityOptions"
              v-model="editForm.address1"
              @change="changeProvince"
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="editForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流信息的弹框 -->
      <el-dialog title="物流详情" :visible.sync="queryVisible">
        <el-timeline>
            <el-timeline-item
            v-for="(item, index) in queryData"
            :key="index"
            :timestamp="item.ftime">
            <p>{{item.context}}</p>
            <p class="el-icon-location" style="color:#0bbd87">{{item.location}}</p>
            </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import cityOptions from './citydata'
import { reqGetOrderList, reqTraceInfo } from "../../api/index";
export default {
  data() {
    return {
      orderList: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 1,
      // 查询物流的弹框
      queryVisible: false,
      queryData:[],
      // 编辑表单的弹框
      editDialogVisible:false,
      editForm:{
        address1:'',
        address2:''
      },
       editRules: {
          address1: [
            { required: true, message: '请选择省市区/县', trigger: 'blur' }
          ],
          address2: [
            { required: true, message: '请输入详细地址', trigger: 'blur' },
          ],
       },
      //  联动城市数据
       cityOptions: cityOptions,
      //  被选中的城市
      city:[]
    }
  },

  created() {
    this.getOrderList();
  },

  methods: {
    //   获取订单的数据
    async getOrderList() {
      const result = await reqGetOrderList(this.queryInfo);
      if (result.meta.status === 200) {
        this.orderList = result.data.goods
        this.total = result.data.total

        console.log(result);
      } else {
        this.$message.error(result.meta.msg);
      }
    },

    // 获取 条数的改变
    handleSizeChange(newSize){
        this.queryInfo.pagesize = newSize
        this.getOrderList()
    },

    // 获取 当前页码值的改变
    handleCurrentChange(newPage){
        this.queryInfo.pagenum = newPage
        this.getOrderList()
    },

    // 点击搜索按钮进行搜索
    searchOrder(){
        console.log(this.queryInfo.query)
        this.getOrderList()
    },

    // 查看物流信息
    async queryLocation(){
        const result = await reqTraceInfo(1106975712662)
        if (result.meta.status === 200) {
        this.queryData = result.data
        console.log(result);
      } else {
        this.$message.error(result.meta.msg);
      }
      this.queryVisible = true
    },

    // 修改地址
    editAddress(row){
      this.editDialogVisible = true
      console.log(row)
    },

    changeProvince(value){
      this.address1 = value[value.length - 1]
    },

    resetForm(){
      this.$refs.editFormRef.resetFields();
      this.city = []
    },

    editSubmit(){
      this.$refs.editFormRef.validate((valid) => {
          if (valid) {
            alert('submit!');
            this.editDialogVisible = false
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      
      
    }
  }
};
</script>
<style lang="less" scoped>
.el-card {
  margin-top: 15px;

  .el-table{
      margin: 15px 0;
      width: 99%;
  }
  .el-cascader{
    width: 100%;
  }

    // 表格的数据居中
  /deep/.el-table th > .cell {
	  text-align: center;
	}
	/deep/.el-table .cell {
	  text-align: center;
	}
}
</style>